package hz.mobileproject.pda.presenter;

import android.util.Log;
import android.widget.Toast;

import com.blankj.utilcode.util.LogUtils;
import com.google.gson.Gson;

import cn.droidlover.xdroidmvp.mvp.XPresent;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;
import hz.mobileproject.pda.entity.request.Request_data;
import hz.mobileproject.pda.entity.request.Request_login_user;
import hz.mobileproject.pda.entity.response.Response_data;
import hz.mobileproject.pda.entity.response.Response_data_login;
import hz.mobileproject.pda.entity.response.Response_login_user;
import hz.mobileproject.pda.network.Api;
import hz.mobileproject.pda.ui.activity.LoginActivity;
import hz.mobileproject.pda.utils.Base64;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2017/7/11 0011.
 */

public class PLogin extends XPresent<LoginActivity>{

    private Gson gson;
    public void loadData(String policeCode,  String password) {
        gson = new Gson();
        Request_login_user request_login_user= new Request_login_user();
        request_login_user.setPoliceCode(policeCode);
        request_login_user.setPassword(password);
        String login_user=  gson.toJson(request_login_user);

        Request_data    data = new Request_data();
        data.setData(Base64.getBase64(login_user));
        String  body_data=  gson.toJson(data);

        RequestBody bodsy=RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),body_data);

        Api.getGankService().getloginData(bodsy)
                .compose(XApi.<Response_data_login>getApiTransformer())
                .compose(XApi.<Response_data_login>getScheduler())
                .compose(getV().<Response_data_login>bindToLifecycle())
                .subscribe(new ApiSubscriber<Response_data_login>() {
                    @Override
                    protected void onFail(NetError error) {
                        LogUtils.e("onFail");
                        getV().showError(error);
                }

                    @Override
                    public void onNext(Response_data_login response_data) {
                        LogUtils.e("onNext");
                        LogUtils.e("PLogin"+Base64.getFromBase64(response_data.getData()));
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        LogUtils.e("onComplete");
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        LogUtils.e("onError");
                    }

                    @Override
                    protected void onStart() {
                        super.onStart();
                        LogUtils.e("onStart");
                    }

                });
    }
}
